package com.atguigu.edustatistics.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.commonutils.R;
import com.atguigu.edustatistics.client.UcenterClient;
import com.atguigu.edustatistics.entity.StatisticsDaily;
import com.atguigu.edustatistics.mapper.StatisticsDailyMapper;
import com.atguigu.edustatistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author jyd
 * @since 2022-04-05
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);
        //通过远程调用获取注册人数
        R registerR = ucenterClient.countRegister(day);
        Integer countRegister = (Integer)registerR.getData().get("countRegister");
        //设置统计分析表的参数，包括注册人数，点赞数，统计日期
        StatisticsDaily staDaily = new StatisticsDaily();
        staDaily.setRegisterNum(countRegister);
        staDaily.setCourseNum(RandomUtils.nextInt(20,50));
        staDaily.setLoginNum(RandomUtils.nextInt(200,400));
        staDaily.setVideoViewNum(RandomUtils.nextInt(100,200));
        staDaily.setDateCalculated(day);
        //添加统计日期的数据
        baseMapper.insert(staDaily);
    }

    //查询图表所需的数据，包括两部分，日期json数组，数量json数组
    @Override
    public Map<String, Object> showData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);
        //因为要返回日期和日期对应的数量
        //前端要求json数组
        //存放日期list
        List<String> dateList = new ArrayList<>();
        //存放数量list
        List<Integer> numList = new ArrayList<>();
        if(type.equals("register_num")){
            for (StatisticsDaily statisticsDaily : staList) {
                dateList.add(statisticsDaily.getDateCalculated());
                numList.add(statisticsDaily.getRegisterNum());
            }
        }else if(type.equals("login_num")){
            for (StatisticsDaily statisticsDaily : staList) {
                dateList.add(statisticsDaily.getDateCalculated());
                numList.add(statisticsDaily.getLoginNum());
            }
        }else if(type.equals("video_view_num")){
            for (StatisticsDaily statisticsDaily : staList) {
                dateList.add(statisticsDaily.getDateCalculated());
                numList.add(statisticsDaily.getVideoViewNum());
            }
        }else{
            for (StatisticsDaily statisticsDaily : staList) {
                dateList.add(statisticsDaily.getDateCalculated());
                numList.add(statisticsDaily.getCourseNum());
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("dataList",dateList);
        map.put("numList",numList);
        return map;
    }
}
